"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MysqlManager = void 0;
const DbConfig_1 = require("../../config/DbConfig");
const MySqlClient_1 = require("./MySqlClient");
const Logger_1 = require("./Logger");
class MysqlManager {
    constructor() {
        // 导出结构不导出数据
        //  /usr/local/mysql/bin/mysqldump --opt -d test -u root -p > ~/test.sql 
        this.dbMap = new Map();
    }
    createClient(dbName, dbType) {
        return __awaiter(this, void 0, void 0, function* () {
            let realDbName = this.getCodeDbName(dbName, dbType);
            let dbConfig = DbConfig_1.DbConfig.getDbConfig(dbType);
            let client = yield MySqlClient_1.MysqlClient.create(dbName, dbConfig.host, dbConfig.port, dbConfig.user, dbConfig.pass);
            this.dbMap.set(realDbName, client);
            return client;
        });
    }
    getCodeDbName(dbName, dbType) {
        let realDbName = dbName + "_db" + dbType;
        return realDbName;
    }
    getDb(dbName, dbType = DbConfig_1.DbConfig.now_db_type) {
        return __awaiter(this, void 0, void 0, function* () {
            let db;
            let realDbName = this.getCodeDbName(dbName, dbType);
            if (this.dbMap.has(realDbName)) {
                db = this.dbMap.get(realDbName);
            }
            else {
                Logger_1.Logger.info("数据库client", dbName);
                db = yield this.createClient(dbName, dbType);
            }
            return db;
        });
    }
}
exports.MysqlManager = MysqlManager;
// 导出数据不导出结构
// mysqldump　-t　数据库名　-uroot　-p　>　xxx.sql
// 导出数据和表结构
// mysqldump　数据库名　-uroot　-p　>　xxx.sql
// 导出特定表的结构
// mysqldump　-uroot　-p　-B　数据库名　--table　表名　>　xxx.sql
MysqlManager.instance = new MysqlManager();
